#include "windows/mainwindow.h"
#include "windows/registerwindow.h"
#include <QApplication>
#include "utils/md5.h"
#include "utils/deviceutils.h"
#include "QTextCodec"
#include "QSharedMemory"
#include "QTime"
#include "QMessageBox"
#include "QString"
#include "windows.h"
#include <Dbghelp.h>
#include "utils/antidebugutils.h"

using namespace std;
int exe(int argc, char *argv[]);

int main(int argc, char *argv[])
{


    STARTUPINFO si;

    GetStartupInfo(&si);

    //    if ((si.dwX != 0) ||(si.dwY != 0) ||(si.dwXCountChars != 0) ||(si.dwYCountChars != 0) ||(si.dwFillAttribute != 0) ||(si.dwXSize != 0) ||(si.dwYSize != 0) ||(si.dwFlags & STARTF_FORCEOFFFEEDBACK))
    //    {
    //    }

#ifdef QT_DEBUG
    exe(argc, argv);
    ;
#else
//    if(!AntiDebugUtils::checkAntiDebug()){
        exe(argc, argv);
//    }
#endif
}

int exe(int argc, char *argv[]){
    QApplication a(argc, argv);
    QTextCodec *codec = QTextCodec::codecForName("TLBBFD385FDA23146702804032A51B");
    QTextCodec::setCodecForLocale(codec);
    QSharedMemory shareMemory;
    shareMemory.setKey("TLBB");
    if(shareMemory.attach()){
        return 0;
    }
    if(shareMemory.create(1)){
        RegisterWindow r;
        r.show();
        return a.exec();
    }else{
        return 0;
    }
}

LONG ApplicationCrashHandler(EXCEPTION_POINTERS *pException){//程式异常捕获
//    /*
//      ***保存数据代码***
//    */
//    //创建 Dump 文件

//    HANDLE hDumpFile = CreateFileW(QTime::currentTime().toString("HH时mm分ss秒zzz.dmp").utf16(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
//    if( hDumpFile != INVALID_HANDLE_VALUE){
//        //Dump信息
//        MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
//        dumpInfo.ExceptionPointers = pException;
//        dumpInfo.ThreadId = GetCurrentThreadId();
//        dumpInfo.ClientPointers = TRUE;
//        //写入Dump文件内容
//        MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);
//    }
//    //这里弹出一个错误对话框并退出程序
//    EXCEPTION_RECORD* record = pException->ExceptionRecord;
//    QString errCode(QString::number(record->ExceptionCode,16)),errAdr(QString::number((qlonglong)record->ExceptionAddress, 16)),errMod;
//    QMessageBox::critical(NULL,"程式崩溃","<FONT size=4><div><b>对于发生的错误，表示诚挚的歉意</b><br/></div>"+
//        QString("<div>错误代码：%1</div><div>错误地址：%2</div></FONT>").arg(errCode).arg(errAdr),
//        QMessageBox::Ok);
//    return EXCEPTION_EXECUTE_HANDLER;
}
